package com.ruoyi.system.controller.feign;


import com.ruoyi.common.core.domain.Parameter;
import com.ruoyi.common.core.domain.entity.SysDictVO;
import com.ruoyi.common.core.utils.Global;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.Sysdeptresult;
import com.ruoyi.system.domain.SysDict;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.BeanUtils;
import com.ruoyi.system.mapper.SysDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/SysDictFeign")
public class SysDictFeignController {

    @Autowired
    public SysDictMapper sysDictMapper;
    @Autowired
    public SysDeptMapper sysdeptMapper;


    @PostMapping("/getSysDict")
    public SysDictVO getSysDict(@RequestParam("parameter")String parameter) {
        SysDictVO sysDict = new SysDictVO();
        com.ruoyi.system.domain.SysDict sourceDict= sysDictMapper.selectDictByLabel(parameter);
        if (sourceDict == null) {
            return null;
        }
        BeanUtils.copyProperties(sourceDict, sysDict);
        return sysDict;
    }
    @PostMapping("/getParameter")
    public Parameter setParameter(@RequestBody Parameter parameter){
        //根据时间查询数据表标识符
        SysDict sysDict = sysDictMapper.selectDictByLabel(parameter.getYear());
        String tablename = Global.DATATABLEPRE + "_" + sysDict.getValue() + Global.TESTWHOLEDATANAME;
        parameter.setTablename(tablename);
        //根据dept_id查询机构信息
        Sysdeptresult sysDept = new Sysdeptresult();
        if (null != parameter.getDeptid() && !"".equals(parameter.getDeptid())){
            sysDept = sysDictMapper.selectDeptById(Long.valueOf(parameter.getDeptid()));
        } else if (null != parameter.getCity() && !"".equals(parameter.getCity())) {
            sysDept = sysDictMapper.selectDeptByName(parameter.getCity());
            parameter.setDeptid(String.valueOf(sysDept.getDeptId()));
        }
        parameter.setOrganizationCode(sysDept.getOrganizationcode());
        return parameter;
    }

    @PostMapping("/getTestPointNum")
    public Map<String,Integer> getTestPointNum(@RequestBody Parameter parameter){
        Integer testPointNum = 0;
        Integer cityTestPointNum = 0;
        //机构代码是全国	总数-2的个数	或者 全国31*3
        if ("00".equals(parameter.getOrganizationCode())) {
            testPointNum = 93;
        } else if (parameter.getOrganizationCode().length() == 2) {
            testPointNum = 3;
        } else if (parameter.getOrganizationCode().length() >= 4) {
            //根据用户选择区域的机构代码，年份，抽样点类型是1（指国测点）的条件查询抽样点个数,去掉所在市是抽样城市，区县不是抽样点所在区县
            testPointNum = sysdeptMapper.getTestPointNum(parameter.getOrganizationCode(),parameter.getYear(),"1");
            //
            if (0 != testPointNum) {
                //查询机构代码的前4位或者前6位是否在城市分类表中，并确定抽样点个数
                Map<String, Integer> cityTestPointNumMap = sysdeptMapper.getCityTestPointNum(parameter.getOrganizationCode(),parameter.getYear());
                if (null != cityTestPointNumMap) {
                    cityTestPointNum = Integer.parseInt(cityTestPointNumMap.get("city_testpoint_num").toString());
                }
            }
        }
        Map<String,Integer> map = new HashMap<>();
        map.put("testPointNum",testPointNum);
        map.put("cityTestPointNum",cityTestPointNum);
        return map;
    }
}
